﻿using System;

namespace Kz.Telegram.Bot.Infrastructure.Extensions
{
    /// <summary>
    /// 时间拓展
    /// </summary>
    public static class TimeExtensions
    {
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetTimeStamp(this DateTime time, int length = 13)
        {
            return (((time.ToUniversalTime().Ticks - 621355968000000000) / 10000000) * 1000).ToString();
        }

        /// <summary>
        /// 获取时间戳(秒)
        /// </summary>
        /// <param name="time"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetTimeStamp2(this DateTime time, int length = 13)
        {
            return (((time.ToUniversalTime().Ticks - 621355968000000000) / 10000000)).ToString();
        }

        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long DateTime2TimeStamp(this DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime TimeStamp2DateTime(this string timeStamp)
        {
            if (timeStamp.Length > 10)
            {
                timeStamp = timeStamp.Substring(0, 10);
            }
            DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dateTimeStart.Add(toNow);
        }

        public static DateTime ConvertDateTime(this string stringDate)
        {
            if (DateTime.TryParse(stringDate, out DateTime _out))
            {
                return _out;
            }
            return DateTime.MinValue;
        }

        /// <summary>
        /// 时间差（毫秒）
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static int GetDiffMilliseconds(this DateTime dt1, DateTime dt2)
        {
            TimeSpan span = dt2.Subtract(dt1);
            return Convert.ToInt32(span.TotalMilliseconds);
        }

        /// <summary>
        /// 时间差（秒）
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static long GetDiffSeconds(this DateTime dt1, DateTime dt2)
        {
            TimeSpan span = dt2.Subtract(dt1);
            return Convert.ToInt64(span.TotalSeconds);
        }


        /// <summary>
        /// 时间差（时分） 00:00
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static string GetDiffMS(this DateTime dt1, DateTime dt2)
        {
            TimeSpan span = dt2.Subtract(dt1);
            var totalSeconds = Convert.ToInt32(span.TotalSeconds);

            var min = totalSeconds / 60;
            var sec = totalSeconds % 60;

            return (min < 10 ? $"0{min}" : min.ToString()) +
                    (sec < 10 ? $"0{sec}" : sec.ToString());
        }

        public static string MinuteSecond(this int totalSeconds)
        {
            var seconds = (int)(totalSeconds % 60);
            var minutes = (int)(totalSeconds / 60) % 60;

          return string.Format("{0:00}:{1:00}", minutes, seconds);

        }

        /// <summary>
        /// 获取指定时间的时间戳（精度到毫秒）表示形式
        /// </summary>
        /// <param name="time">当前时间</param>
        /// <param name="bflag">true 到秒  false 毫秒</param>
        /// <param name="isUtcTime">是否</param>
        /// <returns></returns>
        public static long GetStampFromDateTimeReName(this DateTime time, bool bflag = false, bool isUtcTime = true)
        {
            if (isUtcTime)
                return time.GetUtcTimeStamp(bflag);
            else
                return time.GetTimeStamp(bflag);
        }

        /// <summary>
        /// 非UTC时间  获取指定时间的时间戳（精度到毫秒）表示形式
        /// </summary>
        /// <param name="time">当前时间</param>
        /// <param name="bflag">true 到秒  false 毫秒</param>
        /// <returns></returns>
        private static long GetTimeStamp(this DateTime time, bool bflag = false)
        {
            TimeSpan ts = time - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            if (bflag)
                ret = Convert.ToInt64(ts.TotalSeconds).ToString();
            else
                ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return Convert.ToInt64(ret);
        }

        /// <summary>
        /// UTC时间  获取指定时间的时间戳（精度到毫秒）表示形式
        /// </summary>
        /// <param name="time">当前时间</param>
        /// <param name="bflag">true 到秒  false 毫秒</param>
        /// <returns></returns>
        private static long GetUtcTimeStamp(this DateTime time, bool bflag = false)
        {
            TimeSpan ts = time.AddHours(-8) - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            if (bflag)
                ret = Convert.ToInt64(ts.TotalSeconds).ToString();
            else
                ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return Convert.ToInt64(ret);
        }
    }
}